package com.example.beiwang.tools;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.beiwang.db.record.RecordCursorWrapper;
import com.example.beiwang.db.record.RecordHepler;
import com.example.beiwang.db.record.RecordSchema;
import com.example.beiwang.model.Record;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;



/**
 * 模拟一个存放Record记录的地方，它基本具备增删改查的操作
 * 模拟一个数据库
 * */
public class RecordLab {
    private static RecordLab sRecordLab;
    private static SQLiteDatabase sDatabase;
    private static final String TAG = "RecordLab";

    /**
     * 单例模式模拟数据库
     *
     * @param context 传入activity对象
     * */
    public static RecordLab getLab(Context context){
        if(sRecordLab==null){
            sRecordLab = new RecordLab(context);
        }
        return sRecordLab;
    }

    /**
     * 构造函数执行：
     * 1、数据库的创建
     * 2、打开数据库
     * @param context 传入activity对象
     * */
    public RecordLab(Context context) {
        //执行数据库的创建操作
        RecordHepler recordHepler = new RecordHepler(context);
        //打开一个可写入的数据库
        sDatabase = recordHepler.getWritableDatabase();
        //以下代码，随机生成100条数据
//        for(int i=0;i<100;i++){
//            Record record = new Record();
//            record.setId(i);
//            record.setTitle("测试："+i);
//            record.setFinishDate(new Date());
//            String str = "";
//            for(int j = 0; j<new Random().nextInt(50)+1; j++){
//                str+="备忘录内容";
//            }
//            record.setContent(str);
//            sRecords.add(record);
//        }
    }


    public  List<Record> getRecords() {
        List<Record> records = new ArrayList<>();
        //执行查询SQL语句
        Cursor cursor = sDatabase.query(RecordSchema.TABLE_NAME, null, null, null, null, null, null, null);
        RecordCursorWrapper cursorWrapper = new RecordCursorWrapper(cursor);
        cursorWrapper.moveToFirst();
        do {
            if(!cursorWrapper.isAfterLast()){
                Record record = cursorWrapper.getRecord();
                records.add(record);
            }
        }while (cursorWrapper.moveToNext());
        //关闭游标，也就是数据结果缓存区
        cursorWrapper.close();
        return records;
    }

    public void addRecord(Record record){
        ContentValues contentValues = getContentValues(record);
        //执行插入语句
        sDatabase.insert(RecordSchema.TABLE_NAME,null,contentValues);
    }


    public Record getRecordById(int recordId){
        Cursor cursor = sDatabase.query(RecordSchema.TABLE_NAME, null, "record_id = ?",new String[] {String.valueOf(recordId)}, null, null, null, null);
        RecordCursorWrapper cursorWrapper = new RecordCursorWrapper(cursor);
        cursorWrapper.moveToFirst();
        if(!cursorWrapper.isAfterLast()){
            Record record = cursorWrapper.getRecord();
            //关闭游标，也就是数据结果缓存区
            cursorWrapper.close();
            return record;
        }else{
            return new Record();
        }
    }

    public void updateRecord(Record record){
        ContentValues contentValues = getContentValues(record);
        sDatabase.update(RecordSchema.TABLE_NAME,contentValues,"record_id = ?",new String[] {String.valueOf(record.getId())});
    }


    //定义要插入或要更新的参数
    public ContentValues getContentValues(Record record){
        ContentValues contentValues = new ContentValues();
        //由于record_id字段是自增，也不可改，因此在插入、更新操作时，不需要关注id的变化
        contentValues.put(RecordSchema.Cols.TITLE,record.getTitle());
        contentValues.put(RecordSchema.Cols.CONTENT,record.getContent());
        contentValues.put(RecordSchema.Cols.TEXT_THINKING,record.getTextThinking());
        contentValues.put(RecordSchema.Cols.PICTURE,record.getPicture());
        contentValues.put(RecordSchema.Cols.FINISH_DATE,record.getFinishDate().getTime());
        contentValues.put(RecordSchema.Cols.COMPLETED,record.isCompleted());
        return contentValues;
    }
}